package com.hexacta.hrs.web.struts.actions.projects;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.vectrics.employee.Employee;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.vectrics.user.User;

import com.hexacta.hrs.domain.exchange.Accrued;
import com.hexacta.hrs.domain.exchange.AccruedReferring;
import com.hexacta.hrs.domain.exchange.Confirmation;
import com.hexacta.hrs.domain.staffing.Project;
import com.hexacta.hrs.service.staffing.ProjectService;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.exchange.AccruedService;
import com.opensymphony.xwork2.ActionSupport;

public class ReferringAction extends ActionSupport implements SessionAware, ServletRequestAware{

	private static final long serialVersionUID = 1L;
	
	private HttpServletRequest request;
	private Map<String, Object> session;
	
	private ProjectService projectService;
	private EmployeeService employeeService;
	private AccruedService accruedService;
	
	private String selectedPartnerName;
	private String selectedPartnerId;
	private String selectedProjectName;
	private Long selectedProjectId;
	private String selectedMonth;
	private String selectedYear;
	private String editedAccrueds;
	
	private List<Accrued> accruedsList;
		
	public String filterReferrings(){
		return SUCCESS;
	}
	
	public String updateAccrueds(){
		Integer position = editedAccrueds.indexOf(";");
		Integer begin = 0;
		
		
		while (position != -1) {
			String accruedString = editedAccrueds.substring(begin, position);
			
			try {
				begin = 0;
				Integer endIndex = accruedString.indexOf(",");
				
				Long id = Long.valueOf(accruedString.substring(begin,endIndex));
				begin = endIndex + 1;
				endIndex = accruedString.indexOf(",", endIndex + 1);
				
				Double earn = Double.valueOf(accruedString.substring(begin,
						endIndex));

				begin = endIndex + 1;
				endIndex = accruedString.indexOf(",", endIndex + 1);
				Double bill = Double.valueOf(accruedString.substring(begin,
						endIndex));

				begin = endIndex + 1;
				endIndex = accruedString.indexOf(",", endIndex + 1);
				boolean estimated = Boolean.parseBoolean(accruedString
						.substring(begin, endIndex));

				begin = endIndex + 1;
				endIndex = accruedString.indexOf(",", endIndex + 1);
				String comment = accruedString.substring(begin, endIndex);

				begin = endIndex + 1;
				endIndex = accruedString.indexOf(",", endIndex + 1);
				String userId = accruedString.substring(begin, endIndex);

				Accrued accrued = accruedService.forId(id);
				accrued.setEarn(earn);
				accrued.setBill(bill);
				accrued.setEstimated(estimated);
				accrued.setComment(comment);

				if (!userId.equalsIgnoreCase("undefined")) {
					User user = CoreServiceLocator.getUserService()
							.getUserById(Long.parseLong(userId));

					Employee employee = employeeService.searchBy(user);
					begin = endIndex + 1;

					Calendar date = Calendar.getInstance();
					date.setTimeInMillis(Long.valueOf(accruedString
							.substring(begin)));

					Confirmation confirmation = new Confirmation();
					confirmation.setUser(employee);
					confirmation.setDate(date);

					accrued.setConfirmation(confirmation);
				}
				
				begin = position + 1;
				position = editedAccrueds.indexOf(";", position + 1);

				accruedService.save(accrued);
			} catch (Exception e) {
			}
			
		}
		editedAccrueds = "";
		return SUCCESS;
	}
	
	public List<Employee> getPartnerList(){
		return this.getEmployeeService().getPartners();
	}
	
	public List<Project> getProjectsList(){
		return projectService.findAll();
	}
	
	public List<AccruedReferring> getAccruedsList() {
		try {
			Employee partner = employeeService.searchById(Long.valueOf(selectedPartnerName));
			Integer year = Integer.valueOf(selectedYear);
			Integer month = Integer.valueOf(selectedMonth);
			
			return accruedService.byReferring(partner, year, month);
		} catch (Exception e) {
		}
		return null;
	}

	public String getLoggedUser(){
		Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(getSession());
		return person.getUser().getUserName();
	}
	
	public Long getLoggedUserId(){
		Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(getSession());
		return person.getUser().getId();
	}
	
	public void setAccruedsList(List<Accrued> accruedsList) {
		this.accruedsList = accruedsList;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	
	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public AccruedService getAccruedService() {
		return accruedService;
	}

	public void setAccruedService(AccruedService accruedService) {
		this.accruedService = accruedService;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public String getSelectedPartnerName() {
		return selectedPartnerName;
	}

	public void setSelectedPartnerName(String selectedPartnerName) {
		this.selectedPartnerName = selectedPartnerName;
	}

	public String getSelectedPartnerId() {
		return selectedPartnerId;
	}

	public void setSelectedPartnerId(String selectedPartnerId) {
		this.selectedPartnerId = selectedPartnerId;
	}

	public String getSelectedProjectName() {
		return selectedProjectName;
	}

	public void setSelectedProjectName(String selectedProjectName) {
		this.selectedProjectName = selectedProjectName;
	}

	public Long getSelectedProjectId() {
		return selectedProjectId;
	}

	public void setSelectedProjectId(Long selectedProjectId) {
		this.selectedProjectId = selectedProjectId;
	}

	public String getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(String selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public String getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(String selectedYear) {
		this.selectedYear = selectedYear;
	}

	public String getEditedAccrueds() {
		return editedAccrueds;
	}

	public void setEditedAccrueds(String editedAccrueds) {
		this.editedAccrueds = editedAccrueds;
	}
	
}
